import store from './store'

// 连接websocket
export function createConnection(userID) {
	uni.connectSocket({
		// https://www.wujiaqi.xyz/supermarket
		url: "wss://www.wujiaqi.xyz:8443/supermarket/ws/" + userID,
	});

	uni.onSocketOpen(function(res) {
		console.log('WebSocket连接已打开！');
	});

	uni.onSocketMessage(function(res) {
		console.log('收到服务器内容：' + res.data);
		
		if (res.data.indexOf("您有新的优惠券") == 0) {
			// console.log("以优惠券开头")
			store.dispatch('coupon/getNewCoupon', {msg: res.data})
		} else {
			// 订单信息
			store.dispatch('orderStep/newStep', {msg: res.data})
		}
		
		
	
	});
}

// 关闭
export function closeConnection() {
	uni.closeSocket({
		success() {
			console.log("已关闭socket连接")
		}
	})
}
